<?php
App::uses('AppController', 'Controller');
/**
 * Stages Controller
 *
 * @property Stage $Stage
 */
class StagesController extends AppController {

	/**
	 * index method
	 *
	 * @return void
	 */
	public function index() {
		$this->Stage->recursive = 0;
		$this->set('stages', $this->paginate());
	}

	/**
	 * view method
	 *
	 * @throws NotFoundException
	 * @param string $id
	 * @return void
	 */
	public function view($id = null) {
		$this->Stage->id = $id;
		if (!$this->Stage->exists()) {
			throw new NotFoundException(__('Invalid data'));
		}
		$this->set('stage', $this->Stage->read(null, $id));
	}

	/**
	 * add method
	 *
	 * @return void
	 */
	public function add() {
		$last_stage=$this->Stage->find("first",array("order"=>array("level desc")));
		$level=1;
		if(isset($last_stage["Stage"]["level"]))
		{
			$level =$last_stage["Stage"]["level"]+1;
		}
		if ($this->request->is('post')) {

		 if(isset($this->request->data["Stage"]["user_id"])){
		 	$useId =AuthComponent::user('id');
		 	//$useId ="1";
				$this->request->data["Stage"]["user_id"]=$useId;
		 }
            $this->request->data["Stage"]["level"]=$level;
			$this->Stage->create();
			if ($this->Stage->save($this->request->data)) {
				$id=$this->Stage->id;
				$this->savelog($id);
				$this->Session->setFlash(__('Data has been saved'), 'default',array("class"=>"mws-form-message success"));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Data could not be saved. Please, try again.'), 'default',array("class"=>"mws-form-message error"));
			}
		}
		$users = $this->Stage->User->find('list');
		$this->set(compact('users',"level"));
	}

	/**
	 * edit method
	 *
	 * @throws NotFoundException
	 * @param string $id
	 * @return void
	 */
	public function edit($id = null) {
		$this->Stage->id = $id;
		if (!$this->Stage->exists()) {
			throw new NotFoundException(__('Invalid stage'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {

		 if(isset($this->request->data["Stage"]["user_id"])){
		 	$useId =AuthComponent::user('id');
		 	//$useId ="1";
				$this->request->data["Stage"]["user_id"]=$useId;
		 }

			if ($this->Stage->save($this->request->data)) {
				$this->savelog($id);
				$this->Session->setFlash(__('Data has been saved'), 'default',array("class"=>"mws-form-message success"));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Data could not be saved. Please, try again.'), 'default',array("class"=>"mws-form-message error"));
			}
		} else {
			$this->request->data = $this->Stage->read(null, $id);
		}
		$users = $this->Stage->User->find('list');
		$this->set(compact('users'));
	}

	/**
	 * delete method
	 *
	 * @throws MethodNotAllowedException
	 * @throws NotFoundException
	 * @param string $id
	 * @return void
	 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Stage->id = $id;
		if (!$this->Stage->exists()) {
			throw new NotFoundException(__('Invalid stage'));
		}
		if ($this->Stage->delete()) {
			$this->savelog($id);

			$this->Session->setFlash(__('Data has been deleted'), 'default',array("class"=>"mws-form-message success"));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Data was not deleted'), array('action' => 'index'), array('action' => 'index'));
		$this->redirect(array('action' => 'index'));
	}
}
